package dao

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

var T_hzfgw_record_wp = t_hzfgw_record_wp{}

type t_hzfgw_record_wp struct {
}

/*func (a *t_hzfgw_record_wp) GetLastRid(addr string) (rid uint64) {
	rid = 0
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_record_wp")
	res, err := model.Where(fmt.Sprintf(" addr='%s' ", addr)).Order(" rid desc ").One()
	if err != nil {
		rid = 0
	} else {
		if res != nil {
			rid = gconv.Uint64(res.Map()["rid"])
		}
	}
	return rid
}*/

func (a *t_hzfgw_record_wp) GetLastRid(addr string) (rid uint64) {
	rid = 0
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	re, err := db.GetOne(fmt.Sprintf("SELECT MAX(rid) as rid FROM `t_hzfgw_record_wp` WHERE  addr='%s'", addr))
	if err != nil {
		rid = 0
	} else {
		if re != nil {
			rid = gconv.Uint64(re.Map()["rid"])
		}
	}
	return rid
}

func (a *t_hzfgw_record_wp) SaveList(data interface{}) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	_, err = db.Model("t_hzfgw_record_wp").Data(data).Save()
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}

func (a *t_hzfgw_record_wp) Delete(where ...string) (isOk bool, err error) {
	db := g.DB(g.Cfg().GetString("custom.db_name"))
	model := db.Model("t_hzfgw_record_wp")
	if len(where) == 0 {
		_, err = model.Delete()
	} else {
		_, err = model.Delete(where)
	}
	if err != nil {
		return false, err
	} else {
		return true, nil
	}
}
